package com.awcall.iot.utils;


import com.awcall.iot.common.Tree;
import com.awcall.iot.model.Authority;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class AuthorityTreeUtils {

    public static List<Tree> build(List<Authority> authorityList) {
        Map<Integer, Tree> treeMap = new HashMap<>();
        List<Tree> parentList = authorityList.stream()
                .filter(authority -> authority.getParentId() == null)
                .map(authority -> {
                    Tree tree = new Tree(authority);
                    treeMap.put(tree.getId(), tree);
                    return tree;
                })
                .collect(Collectors.toList());
        childrenList(authorityList, treeMap);
        return parentList;
    }


    private static void childrenList(List<Authority> authorityList, Map<Integer, Tree> treeMap) {
        if (treeMap.isEmpty()) {
            return;
        }

        Map<Integer, Tree> tmpMap = new HashMap<>();
        authorityList.stream()
                .filter(authority -> authority.getParentId() != null)
                .filter(authority -> treeMap.containsKey(authority.getParentId()))
                .forEach(authority -> {
                    Tree parentTree = treeMap.get(authority.getParentId());
                    Tree tree = new Tree(authority);
                    parentTree.addChildren(tree);
                    tmpMap.put(tree.getId(), tree);
                });
        childrenList(authorityList, tmpMap);
    }

    public static void main(String[] args) {
        List<Authority> authorityList = new ArrayList<>();
        authorityList.add(new Authority()
                .setId((int) 1L)
                .setAuthorityName("1")
                .setParentId((int) 0L)
        );
        authorityList.add(new Authority()
                .setId((int) 21L)
                .setAuthorityName("21")
                .setParentId((int) 2L)
        );
        authorityList.add(new Authority()
                .setId((int) 22L)
                .setAuthorityName("22")
                .setParentId((int) 2L)
        );
        authorityList.add(new Authority()
                .setId((int) 2L)
                .setAuthorityName("11")
                .setParentId((int) 0L)
        );

        authorityList.add(new Authority()
                .setId((int) 3L)
                .setAuthorityName("12")
                .setParentId((int) 0L)
        );
        authorityList.add(new Authority()
                .setId((int) 31L)
                .setAuthorityName("31")
                .setParentId((int) 3L)
        );
        authorityList.add(new Authority()
                .setId((int) 311L)
                .setAuthorityName("311")
                .setParentId((int) 31L)
        );
        authorityList.add(new Authority()
                .setId((int) 312L)
                .setAuthorityName("312")
                .setParentId((int) 31L)
        );

        authorityList.add(new Authority()
                .setId((int) 4L)
                .setAuthorityName("13")
                .setParentId((int) 0L)
        );

        long now = System.currentTimeMillis();
        List<Tree> treeList = build(authorityList);
        System.out.println(JsonUtils.toJson(treeList));
        System.out.println(System.currentTimeMillis() - now);
    }


}
